﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using PGI.EN.Categoria;

namespace PGI.AD.Categoria
{
    public class CategoriaAd
    {
        public static List<CategoriaEn> Categorias_Padres()
        {
            SqlCommand command = null;
            SqlConnection connection = null;
            SqlDataReader reader = null;

            string sql = "MntCategoriasSPadres";

            try
            {
                connection = DataBaseFactory.SqlOpenDatabase("PGI");

                command = new SqlCommand(sql, connection);
                command.CommandType = CommandType.StoredProcedure;

                reader = command.ExecuteReader();
                List<CategoriaEn> lstCategorias = new List<CategoriaEn>();
                CategoriaEn categEn = null;

                while (reader.Read())
                {

                    categEn = new CategoriaEn();
                    categEn.CategoriaID = reader.IsDBNull(0) ? 0 : Convert.ToInt32(reader.GetValue(0));
                    categEn.SubCategoriaID = reader.IsDBNull(1) ? 0 : Convert.ToInt32(reader.GetValue(1));
                    categEn.Descripcion = reader.IsDBNull(2) ? string.Empty : reader.GetValue(2).ToString();
                    categEn.Estado = reader.IsDBNull(3) ? 0 : Convert.ToInt32(reader.GetValue(3));

                    lstCategorias.Add(categEn);
                }

                return lstCategorias;
            }

            catch (SqlException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            finally
            {
                if (reader != null)
                    reader.Close();
                DataBaseFactory.SqlCloseDatabase("PGI");
            }
        }

        public static int InsertarCategoria(CategoriaEn _categoriaEn)
        {
            SqlCommand command = null;
            SqlConnection connection = null;
            SqlDataReader reader = null;

            string sql = "MntCategoriaI";

            try
            {
                connection = DataBaseFactory.SqlOpenDatabase("PGI");

                command = new SqlCommand(sql, connection);
                command.CommandType = CommandType.StoredProcedure;
                
                command.Parameters.AddWithValue("@SubCategoriaID", _categoriaEn.SubCategoriaID);
                command.Parameters.AddWithValue("@Descripcion", _categoriaEn.Descripcion);
                command.Parameters.AddWithValue("@UsuarioID", _categoriaEn.UsuarioID);
                command.Parameters.Add("@Resultado", SqlDbType.Int).Direction = ParameterDirection.Output;
                
                command.ExecuteNonQuery();

                return Convert.ToInt32(command.Parameters["@Resultado"].Value);
            }

            catch (SqlException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            finally
            {
                if (reader != null)
                    reader.Close();
                DataBaseFactory.SqlCloseDatabase("PGI");
            }
        }

        public static int EliminarCategoria(int _categoriaID, int _usuarioID)
        {
            SqlCommand command = null;
            SqlConnection connection = null;
            SqlDataReader reader = null;

            string sql = "MntCategoriaE";

            try
            {
                connection = DataBaseFactory.SqlOpenDatabase("PGI");

                command = new SqlCommand(sql, connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add("@CategoriaID", SqlDbType.Int).Direction = ParameterDirection.Input;
                command.Parameters.Add("@UsuarioID", SqlDbType.Int).Direction = ParameterDirection.Input;
                command.Parameters.Add("@Resultado", SqlDbType.Int).Direction = ParameterDirection.Output;

                command.Parameters["@CategoriaID"].Value = _categoriaID;
                command.Parameters["@UsuarioID"].Value = _usuarioID;

                command.ExecuteNonQuery();

                return Convert.ToInt32(command.Parameters["@Resultado"].Value);
            }

            catch (SqlException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            finally
            {
                if (reader != null)
                    reader.Close();
                DataBaseFactory.SqlCloseDatabase("PGI");
            }
        }

        public static int EditarCategoria(CategoriaEn _categoriaEn)
        {
            SqlCommand command = null;
            SqlConnection connection = null;
            SqlDataReader reader = null;

            string sql = "MntCategoriaU";

            try
            {
                connection = DataBaseFactory.SqlOpenDatabase("PGI");

                command = new SqlCommand(sql, connection);
                command.CommandType = CommandType.StoredProcedure;
                                
                command.Parameters.AddWithValue("@CategoriaID", _categoriaEn.CategoriaID);                
                command.Parameters.AddWithValue("@Descripcion", _categoriaEn.Descripcion);
                command.Parameters.AddWithValue("@UsuarioID", _categoriaEn.UsuarioID);
                command.Parameters.Add("@Resultado", SqlDbType.Int).Direction = ParameterDirection.Output;
                
                command.ExecuteNonQuery();

                return Convert.ToInt32(command.Parameters["@Resultado"].Value);
            }

            catch (SqlException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            finally
            {
                if (reader != null)
                    reader.Close();
                DataBaseFactory.SqlCloseDatabase("PGI");
            }
        }
    }
}
